/**
 * 
 * ......................我佛慈悲......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *                       
 *..................佛祖开光 ,永无BUG...................
 * 
 */
class AchievementUI extends UIBase{
	private btn_back:BaseButton;
	private achievement_scroller:eui.Scroller;
	private achievement_list:eui.List;
	private _listDataArr:eui.ArrayCollection;
	private _info;
	public constructor(data?) {
		super('AchievementView',data);
	}

	public init()
	{
		super.init(true);
		this.requsetInfo();
	}

	public addEvent()
	{
		this.btn_back.addTapHandler(this.close, this);
	}

	/**
	 * 请求成就数据
	 */
	public requsetInfo()
	{
		let sendData = {
			version:GameConst.version
		}
		UIManager.instance().showNormalLoading();
		HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/achieve_info' , StringUtil.jsonToHttpPostString(sendData), (e)=>{
			let data = JSON.parse(e);
			UIManager.instance().hideNormalLoading();
			if(data && data.msg == 'ok')
			{
				this._info = data.data;
				this.initUI();
			}
		})
	}

	public initUI()
	{
		let obj = DataJson.achievement_define;
		let arr = [];
		for(let k in obj)
		{
			let value = JSON.parse(JSON.stringify(obj[k]));
			value.Info = this.getConditionState(this._info[k]);
			arr.push(value);
		}
		arr.sort(function(a, b){
			return b.Info.curState - a.Info.curState;
		});
		this._listDataArr = new eui.ArrayCollection(arr);
		this.achievement_list.itemRenderer = AchievementItemUI;
		this.achievement_list.dataProvider = this._listDataArr;
	}

	public getConditionState(process:string)
	{
		let data = {
			curState:1,
			curNum:0
		}
		let arr = process.split('_');
		let state = arr[arr.length - 1];
		if(state == 'complete')
		{
			//完成 未领取
			data.curState = 3;
		}
		else if(state == 'get')
		{
			//已领取
			data.curState = 1;
		}
		else
		{
			//未完成
			data.curState = 2;
			data.curNum = parseInt(state);
		}
		return data;
	}

	public close()
	{
		this.remove(1);
	}

	public removeEvent()
	{
		this.btn_back.removeTapHandler();
	}

	public clear()
	{
		super.clear();
		this._listDataArr = null;
		this._info = null;
		this.achievement_list.dataProvider = null;
		this.achievement_list.itemRenderer = null;
	}
}